package com.guigusuqi.survey.service.impl;

import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.survey.entity.Question;
import com.guigusuqi.survey.entity.Questionnaire;
import com.guigusuqi.survey.entity.UserQuestionnaire;
import com.guigusuqi.survey.mapper.QuestionMapper;
import com.guigusuqi.survey.mapper.QuestionnaireMapper;
import com.guigusuqi.survey.mapper.UserQuestionnaireMapper;
import com.guigusuqi.survey.service.QuestionnaireService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author suqi
 * @since 2022-04-12
 */
@Service
public class QuestionnaireServiceImpl extends ServiceImpl<QuestionnaireMapper, Questionnaire> implements QuestionnaireService
{
    @Resource
    private QuestionnaireMapper questionnaireMapper;

    @Resource
    private UserQuestionnaireMapper userQuestionnaireMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Override
    public Result findQuestion()
    {
        List<Question> questions = questionnaireMapper.findQuestion();

        return Result.success().data("rows",questions);
    }

    @Override
    @Transactional
    public Boolean publishQuestionnaireByQuestionnaireId(String openid,  List<Map> questionnaireParams)
    {
        //存每条数据库的插入记录是否成功
        int[] isSuccess = new int[questionnaireParams.size()];

        int j = 0;
        for (Map questionnaireParam : questionnaireParams)
        {
            Iterator<Map.Entry<String, String>> it = questionnaireParam.entrySet().iterator();
            while (it.hasNext())
            {
                Map.Entry<String, String> entry = it.next();
                //获取用户回答的这道题的id以及答案
                String questionId = entry.getKey();
                String questionScore = entry.getValue();// 问题的结果
                String userAnswer = null;

                //判断有没有这个问题
                Question question = questionMapper.selectById(questionId);
                if(Objects.isNull(question))
                {
                    return false;
                }

                //获得这个题目是单选题还是填空题
                String type = question.getType();
                if(type.equals("填空题"))
                {
                    userAnswer = questionScore;
                    questionScore = "0";
                }else
                {
                    switch (questionScore)
                    {
                        case "1":userAnswer = "A";break;
                        case "2":userAnswer = "B";break;
                        case "3":userAnswer = "C";break;
                        case "4":userAnswer = "D";break;
                        case "5":userAnswer = "E";break;
                    }
                }
                if(StringUtils.isNotEmpty(questionId) && StringUtils.isNotEmpty(questionScore) && StringUtils.isNotEmpty(userAnswer))
                {
                    UserQuestionnaire userQuestionnaire = new UserQuestionnaire();
                    userQuestionnaire.setOpenid(openid);
                    userQuestionnaire.setQuestionId(questionId);
                    userQuestionnaire.setUserAnswer(userAnswer);
                    userQuestionnaire.setQuestionScore(Integer.parseInt(questionScore));
                    int i = userQuestionnaireMapper.insert(userQuestionnaire);
                    isSuccess[j++] = i;
                }else
                {
                    throw new RuntimeException("问卷传参错误！");
                }
            }
        }
        for (int success : isSuccess)
        {
            if(success < 1)
            {
                return false;
            }
        }
        return true;
    }
}
